<?php
class Work_facility extends CI_Controller 
{
	private $limit = 10;
	function __construct()
	{
		parent::__construct();
		if((!check_auth()) ? redirect("/login/auth") : site_url("/home"));
		$this->load->model('work_facility_model');
	}

	/*********************************************************
	**	MODULE ID Card and Telephone						**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function id_card_and_telephone()
	{

		if(count($_POST) > 0)
		{
			$this->form_validation->set_rule();

		}
		else
		{
			$data['dtNow'] = date("d/m/Y",now());
			$data['workPer'] = $this->work_facility_model->getWorkPermission();
			$data['floor_active'] = $this->work_facility_model->arr_floor();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	/*********************************************************
	**	MODULE Parking Lot									**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function parking_lot()
	{
		if(count($_POST) > 0)
		{	
			$this->form_validation->set_rules('Emp_Name','Name','trim|required');
			if($this->input->post('v_car') != "")
			{
				$Lcs_1 = $this->input->post('License1');
				$Lcs_2 = $this->input->post('License2');
				$Lcs_3 = $this->input->post('License3');
				$Cert_1 = $this->input->post('Certificate1');
				$Cert_2 = $this->input->post('Certificate2');
				$Cert_3 = $this->input->post('Certificate3');
				if($Lcs_1 != "")
				{
					if($Cert_1 != "")
					{
						if($this->checkCar_License($Lcs_1) == FALSE)
						{
							redirect($_SERVER['HTTP_REFERER']);
						}			
					}
					else
					{
						$Error = array('danger' => 'Vehicle Number Certificate No. 1 must be fill');
						$this->session->set_userdata($Error);
						redirect($_SERVER['HTTP_REFERER']);
					}
					
					if($Lcs_2 != "")
					{
						if($Cert_2 != "")
						{
							if($this->checkCar_License($Lcs_2) == FALSE)
							{
								redirect($_SERVER['HTTP_REFERER']);
							}			
						}
						else
						{
							$Error = array('danger' => 'Vehicle Number Certificate No. 2 must be fill');
							$this->session->set_userdata($Error);
							redirect($_SERVER['HTTP_REFERER']);
						}	
					}
					
					if($Lcs_3 != "")
					{
						if($Cert_3 != "")
						{
							if($this->checkCar_License($Lcs_3) == FALSE)
							{
								redirect($_SERVER['HTTP_REFERER']);
							}		
						}
						else
						{
							$Error = array('danger' => 'Vehicle Number Certificate No. 3 must be fill');
							$this->session->set_userdata($Error);
							redirect($_SERVER['HTTP_REFERER']);
						}
					}
					
				}
				else
				{
					$Error = array('danger' => 'Form No. 1 should not be empty');
					$this->session->set_userdata($Error);
					redirect($_SERVER['HTTP_REFERER']);
				}
			}
			
			if($this->form_validation->run() == FALSE)
			{
				
			}
		}
		else
		{
			$data = 0;
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	function checkCar_License($pol)
	{
		$a_pol = substr_count($pol, ' ');
		if ($a_pol == 2)
		{
			$b_pol = explode(' ',$pol);
			if($this->alpha1($b_pol[0]) != TRUE)
			{
				$Error = array('danger' => 'Format is wrong ! First character must be alphabet (max 2 Characters)');
				$this->session->set_userdata($Error);
				return FALSE;
			}
			
			if($this->numeric($b_pol[1]) != TRUE)
			{
				$Error = array('danger' => 'Format is wrong ! Second character must be Number (max 4 Characters)');
				$this->session->set_userdata($Error);
				return FALSE;
			}
			
			if($this->alpha2($b_pol[2]) != TRUE)
			{
				$Error = array('danger' => 'Format is wrong ! Third character must be alphabet (max 3 Characters)');
				$this->session->set_userdata($Error);
				return FALSE;
			}
		}
		else
		{
			$Error = array('danger' => 'Format is wrong');
			$this->session->set_userdata($Error);
			return FALSE;
		}
		return TRUE;
	}

	function validated_data($c)
	{
		if ($c == 1)
		{
			$this->form_validation->set_rules('Pol1','Vehicle License Number','callback_alpha_numeric_space|xss_clean');
			$this->form_validation->set_rules('License1','Vehicle Number Certificate No. 1','alpha_numeric_space|required|xss_clean');
			if(!$this->input->post('Pol2') == "")
			{
				$this->form_validation->set_rules('Pol2','Vehicle License Number','alpha_numeric_space|min_length[7]|max_length[9]|xss_clean');
				$this->form_validation->set_rules('License2','Vehicle Number Certificate No. 2','alpha_numeric_space|required|xss_clean');
			}
			
			if(!$this->input->post('Pol3') == "")
			{
				$this->form_validation->set_rules('Pol3','Vehicle License Number','alpha_numeric_space|min_length[7]|max_length[9]|xss_clean');
				$this->form_validation->set_rules('License3','Vehicle Number Certificate No. 3','alpha_numeric_space|required|xss_clean');
			}			
			if ($this->form_validation->run() !== false)
			{
				$Error = array('danger' => 'pass');
				$this->session->set_userdata($Error);
				redirect($_SERVER['HTTP_REFERER']);
			}
			else
			{
				$Error = array('danger' => validation_errors());
				$this->session->set_userdata($Error);
				redirect($_SERVER['HTTP_REFERER']);
			}
		}
	
	}
	
	function numeric($str)
	{
		return (! preg_match('/^[0-9]{1,4}$/',$str)) ? FALSE : TRUE;
	}
	
	function alpha1($str)
	{
		return ( ! preg_match("/^([a-z]){1,2}$/", $str)) ? FALSE : TRUE;
	}
	
	function alpha2($str)
	{
		return ( ! preg_match("/^([a-z]){1,3}$/", $str)) ? FALSE : TRUE;
	}
	
	
	/*********************************************************
	**	MODULE Overtime										**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/	
	function overtime()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{
			$data['dtNow'] = date("d/m/Y",now());
			$data['ShowFloor'] = $this->work_facility_model->getFloor();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	/*********************************************************
	**	MODULE Repair And Maintenance						**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/	
	function repair_and_maintenance()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{
			$data['dtNow'] = date("d/m/Y",now());
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}

	/*********************************************************
	**	MODULE Request GA									**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function request_ga()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{	
			$data['dtNow'] = date("d/m/Y",now());
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}

	/*********************************************************
	**	MODULE Work Permission								**
	**	AUTHOR 			: ERY								**
	**	DATE CREATED	: 19/05/2014						**
	*********************************************************/
	function work_permission()
	{
		if(count($_POST) > 0)
		{
		
		}
		else
		{	
			$data['dtNow'] = date("d/m/Y",now());
			$data['ShowFloor'] = $this->work_facility_model->getFloor();
			$this->getView($this->uri->segment(1),$this->uri->segment(2),$data);
		}
	}
	
	/*********************************************************
	**	MODULE GLOBALS	Get and Search NIK					**
	**	AUTHOR 			: ERY								**
	**	CREATED	DATE	: 19/05/2014						**
	*********************************************************/
	/*=======================================================
	 Function 		: Get All Data Employee
	 AUTHOR			: ERY
	 CREATED DATE	: 19/05/2014
	=======================================================*/
	/*=======================================================
	 MODIFIED DATE	: 22/05/2014
	 MODIFIED BY	: ERY
	 =======================================================*/	
	function globals_nik_Search()
	{
		$Emp_Name[] = SearchDetailEmp($this->security->xss_clean($this->input->post('SearchName')));
		foreach($Emp_Name AS $Emp_Name =>$keyVal)
		{
			$data['Show_Name'] = $keyVal['arr_Name'];
		}
		echo json_encode($data);
	}
	
	/*=======================================================
	 Function 		: Get employee data by search ID
	 				  from function_helper
	 AUTHOR			: ERY
	 CREATED DATE	: 19/05/2014
	=======================================================*/
	/*=======================================================
	 MODIFIED DATE	: 22/05/2014
	 MODIFIED BY	: ERY
	 =======================================================*/
	function globals_nik_detail()
	{
		$data['DetailEmp'] = putDetailEmp($this->security->xss_clean($this->input->post('id')));
		echo json_encode($data);
	}

	/*=======================================================
	 Function 		: Load static header and footer view page 
	 				  and dinamic page view page from each 
					  function 
	 AUTHOR			: ERY
	 CREATED DATE	: 19/05/2014
	=======================================================*/	
	function getView($P_Menu,$S_Menu, $data)
	{
		$this->load->view('header');
		if ($data != 0)
		{
			$this->load->view($P_Menu.'/'.$S_Menu,$data);
		}
		else
		{
			$this->load->view($P_Menu.'/'.$S_Menu);
		}
		$this->load->view('footer');
	}
}
?>